--[[
	碰撞管理，导出当成全局变量、函数用
]]

local Pos2EventMap = {}
cc.exports.Pos2EventMap = Pos2EventMap

local function makePosKey(x, y)
	return string.format("%d, %d", x, y)
end

local function SetCollide(x, y, event)
	if nil ~= event.Name then
		for key, ev in pairs(Pos2EventMap) do
			if ev.Name == event.Name then
				Pos2EventMap[key] = nil
			end
		end
	end

	local key = makePosKey(x, y)
	if Pos2EventMap[key] ~= nil then
		return
	end

	Pos2EventMap[key] = event
end
cc.exports.SetCollide = SetCollide

local function CheckCollide(x, y)
	return Pos2EventMap[makePosKey(x, y)]	
end
cc.exports.CheckCollide = CheckCollide

local function ResetCollide( )
	Pos2EventMap = {}
end
cc.exports.ResetCollide = ResetCollide

local function RandomEmptyPos( bound )
	local genBoundLimit = bound - 1
	local x, y = 0, 0

	while true do
		x = math.random(-genBoundLimit, genBoundLimit)
		y = x
		if nil == CheckCollide(x, y) then
			--在死循环中找到之前TABLE里没有的X,Y值跳出，说明找到了新的随机值
			--这样得到的随机值就与之前的不一样了
			break
		end
	end

	return x, y
end
cc.exports.RandomEmptyPos = RandomEmptyPos